package com.kelvin.paopao.module.market.market.takeorder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.kelvin.kelvinlib.imageloader.GlideUtil;
import com.kelvin.kelvinlib.utils.LogUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.api.responseBean.GoodDetailModel;
import com.kelvin.paopao.api.responseBean.LocationPickModel;
import com.kelvin.paopao.injector.components.DaggerTakeOrderComponent;
import com.kelvin.paopao.injector.modules.TakeOrderModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.market.market.locationpick.LocationPickActivity;
import com.kelvin.paopao.widgets.CustomToolbar;
import com.kelvin.paopao.widgets.NumberAddSubView;
import com.kelvin.paopao.widgets.SelectPayWayView;
import com.kelvin.paopao.widgets.dialog.OptionSelector;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/11.
 */

public class TakeOrderActivity extends BaseActivity<TakeOrderPresenter> implements ITakeOrderView {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.iv_good)
    ImageView ivGood;
    @BindView(R.id.tv_good_name)
    TextView tvGoodName;
    @BindView(R.id.tv_good_price)
    TextView tvGoodPrice;
    @BindView(R.id.add_sub_view)
    NumberAddSubView addSubView;
    @BindView(R.id.select_pay_way)
    SelectPayWayView selectPayWay;
    @BindView(R.id.tv_pay_money)
    TextView tvPayMoney;
    @BindView(R.id.tv_send_time)
    TextView tvSendTime;

    private String id, number, aId;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_take_order;
    }

    @Override
    protected void initInjector() {
        DaggerTakeOrderComponent.builder().takeOrderModule(new TakeOrderModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        id = getIntent().getStringExtra("id");
        number = getIntent().getStringExtra("number");

        customToolbar.showBackIcon(this);
        addSubView.setValue(Integer.parseInt(number));
        addSubView.setOnButtonClickListener(new NumberAddSubView.OnButtonClickListener() {
            @Override
            public void onButtonAddClick(View view, int value) {
                if (goodDetailModel != null) {
                    tvPayMoney.setText(String.valueOf(calculteMoney(goodDetailModel.getSMoney())));
                }
            }

            @Override
            public void onButtonSubClick(View view, int value) {
                if (goodDetailModel != null) {
                    tvPayMoney.setText(String.valueOf(calculteMoney(goodDetailModel.getSMoney())));
                }
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(id);
    }

    @OnClick({R.id.ll_select_time, R.id.tv_pay, R.id.rl_location})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_select_time:
                OptionSelector.showSendGoodTimeSeletor(this).setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int option1, int option2, int option3) {
                        tvSendTime.setText(OptionSelector.sendGoodTime.get(option1));
                    }
                });
                break;
            case R.id.tv_pay:
                if (TextUtils.isEmpty(aId)) {
                    ToastUtil.showToast(this, "请选择送货地址");
                    return;
                }
                if (Integer.parseInt(number) == 0) {
                    return;
                }
                String oPayMode = String.valueOf(selectPayWay.getSelectedWay());
                mPresenter.takeOrder(id, aId, number, oPayMode, null);
                break;
            case R.id.rl_location:
                LocationPickActivity.launchForResult(this);
                break;
        }
    }

    public static void launch(Context context, String id, String number) {
        Intent intent = new Intent(context, TakeOrderActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("number", number);
        context.startActivity(intent);
    }

    @Override
    public void showLocation(LocationPickModel model) {
        tvName.setText(model.getUsername());
        tvPhone.setText(model.getTel());
        tvLocation.setText(model.getAddress());
        LogUtil.d("id:" + model.getId());
        aId = String.valueOf(model.getId());
    }

    private GoodDetailModel goodDetailModel;

    @Override
    public void showGoodDetail(GoodDetailModel model) {
        this.goodDetailModel = model;
        tvGoodName.setText(model.getSName());
        GlideUtil.loadCommonBG(model.getSImg(), R.drawable.default_img, ivGood, this);
        tvGoodPrice.setText(model.getSMoney() + "");
        tvPayMoney.setText(String.valueOf(calculteMoney(model.getSMoney())));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == LocationPickActivity.REQUEST_CODE_TO_LP) {
                LocationPickModel model = (LocationPickModel) data.getSerializableExtra("pick");
                if (model != null) {
                    aId = String.valueOf(model.getId());
                    tvName.setText(model.getUsername());
                    tvPhone.setText(model.getTel());
                    tvLocation.setText(model.getLname()+"附近");
                }
            }
        }
    }

    /**
     * 计算订单价格
     */
    private double calculteMoney(double money) {
        number = String.valueOf(addSubView.getValue());
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(money));
        return bigDecimal.multiply(new BigDecimal(number)).doubleValue();
    }
}
